package com.huaifang.yan.file;

import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;

public class BufferFile {
    public static void main(String[] args) {

        ClassLoader classLoader= BufferFile.class.getClassLoader();
        try(FileChannel fileChannel = new FileInputStream(classLoader.getResource("test.txt").getPath()).getChannel()){
            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
            while (fileChannel.read(byteBuffer) != -1) {
                byteBuffer.flip();
                while (byteBuffer.hasRemaining()) {
                    //log.info(new String(byteBuffer.array(), StandardCharsets.UTF_8));
                   System.out.println((char) byteBuffer.get());
                }
                byteBuffer.clear();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
